package com.dx.mes.common.dao.impl;


import com.dx.common.table.ppt.Bis_ope;
import com.dx.common.table.ppt.Bis_opeId;
import com.dx.common.table.ppt.Bis_path;
import com.dx.common.table.ppt.Bis_pathId;
import com.dx.mes.common.core.dao.impl.GenericHibernateDao;
import com.dx.mes.common.dao.IBisOpeDao;
import com.dx.mes.common.dao.IBisPathDao;
import com.dx.mes.common.table.rtnBean.RouteObj;
import com.dx.mes.common.transcoder.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import static com.dx.mes.common.bean.def.GenericDef.*;

@Repository
public class BisPathHibernateDao extends GenericHibernateDao<Bis_path, Bis_pathId> implements IBisPathDao {
	@Autowired
	private IBisOpeDao bisOpeDao;

	@Override
	public RouteObj getNxPath(String pathID, String pathVer, String crOpeNO) {
		RouteObj routeObj = new RouteObj();
		Bis_path bis_path = new Bis_path();
		Bis_path nx_bis_path = new Bis_path();
		Bis_pathId bis_pathId = new Bis_pathId();
		Bis_pathId nx_bis_pathId = new Bis_pathId();
		Bis_ope nx_bis_ope = new Bis_ope();
		Bis_opeId nx_bis_opeId = new Bis_opeId();
		
		// Get current path
		bis_pathId.setPath_id_fk(pathID);
		bis_pathId.setPath_ver_fk(pathVer);
		bis_pathId.setCr_ope_no(crOpeNO);
		bis_path = this.get(bis_pathId);
		if (null == bis_path) {
			routeObj.setGszMsgBuf("Not found current path ["
					+ bis_pathId.toString() + "] in ["
					+ Bis_path.class.getSimpleName().toUpperCase() + "]");
			routeObj.setLRc(E_BIS_PATH + E_READ_NOT_FOUND);
			return routeObj;
		}

		// Check last ope
		if (Util.isSpaceCheck(bis_path.getNx_ope_no())) {
			routeObj.setGszMsgBuf("Path [" + bis_pathId.toString()
					+ "] is the last ope of this path.");
			routeObj.setLRc(PATH_IS_FINISH);
			return routeObj;
		}
		
		// Get next path
		nx_bis_pathId.setPath_id_fk(bis_path.getId().getPath_id_fk());
		nx_bis_pathId.setPath_ver_fk(bis_path.getId().getPath_ver_fk());
		nx_bis_pathId.setCr_ope_no(bis_path.getNx_ope_no());
		nx_bis_path = this.get(nx_bis_pathId);
		if(null == nx_bis_path){
			routeObj.setGszMsgBuf("Not found next path ["
					+ nx_bis_pathId.toString() + "] in ["
					+ Bis_path.class.getSimpleName().toUpperCase() + "]");
			routeObj.setLRc(E_BIS_PATH + E_READ_NOT_FOUND);
			return routeObj;
		}
		
		// Get next ope
		nx_bis_opeId.setOpe_id(nx_bis_path.getCr_ope_id_fk());
		nx_bis_opeId.setOpe_ver(nx_bis_path.getCr_ope_ver_fk());
		nx_bis_ope = bisOpeDao.get(nx_bis_opeId);
		if(null == nx_bis_ope){
			routeObj.setGszMsgBuf("Not found next ope ["
					+ nx_bis_opeId.toString() + "] in ["
					+ Bis_ope.class.getSimpleName().toUpperCase() + "]");
			routeObj.setLRc(E_BIS_OPE + E_READ_NOT_FOUND);
			return routeObj;
		}
		
		
		routeObj.setPath(nx_bis_path);
		routeObj.setOpe(nx_bis_ope);
		routeObj.setLRc(_NORMAL);
		
		return routeObj;
	}

	@Override
	public RouteObj getNxPath(Bis_path bis_path) {
		return this.getNxPath(bis_path.getId().getPath_id_fk(), bis_path.getId().getPath_ver_fk(),  bis_path.getId().getCr_ope_no());
		}

	@Override
	public Bis_path getPathOpeInfo(String path_id_fk, String path_ver_fk, String cr_ope_no) {
		Bis_pathId id = new Bis_pathId();
		id.setPath_id_fk(path_id_fk);
		id.setPath_ver_fk(path_ver_fk);
		id.setCr_ope_no(cr_ope_no);
		return this.get(id);
	}
	
}
